﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Text;
using EKAABSInformationManagement.Database;


namespace EKAABSInformationManagement.Service
{
    public class NetworkService
    {
        private EKAASBIMEntities dbContext;
        private static readonly object lock_flag = new object();
        public NetworkService()
        {
            dbContext = new EKAASBIMEntities();
        }
        public List<networkInfo> getAllNetworkInfo()
        {
            List<network> orgList = dbContext.network.ToList();
            List<networkInfo> curList=new List<networkInfo>();
            foreach(network n in orgList)
            {
                networkInfo netInfo = new networkInfo();
                var d = dbContext.asset.SingleOrDefault(o => o.assetID == n.assetID);
                if (d != null)
                {
                    if (d.belongto == null)
                    {
                        netInfo.Alias = "暂无OWNER";
                    }
                    else
                    {
                        netInfo.Alias = d.belongto;
                    }
                    netInfo.AssetID = n.assetID;
                    netInfo.LatestUpdateDate = n.latestUpdateDate;
                    netInfo.MAC = n.MAC;
                    netInfo.Status = n.status;
                    curList.Add(netInfo);
                }
            }
            return curList;
        }
        public List<network> getAllOverduedInfo()
        {
            var query = from n in dbContext.network
                        where n.status == 3
                        select n;
            return query.ToList();
        }
        public network getNetworkByAssetID(string assetID)
        {
            var query = from a in dbContext.network
                        where a.assetID == assetID
                        select a;
            return query.FirstOrDefault();
        }
        public List<requirementRecords> getAllRequirementRecords()
        {
            return dbContext.requirementRecords.ToList();
        }
        public void insert(network network, out bool b)
        {
            b = false;
            network.latestUpdateDate = DateTime.Now;
            if (dbContext.network.SingleOrDefault(o => o.MAC == network.MAC) != null)
            {
                b = true;
                return;
            }
            dbContext.network.AddObject(network);
            dbContext.SaveChanges();
        }
        public void delete(string[] MACList)
        {
            foreach (string MAC in MACList)
            {
                var d = dbContext.network.SingleOrDefault(o => o.MAC == MAC);
                if (d != null)
                {
                    dbContext.DeleteObject(d);
                }
            }
            dbContext.SaveChanges();
        }
        public void update(networkInfo n)
        {
            network oldNetwork = dbContext.network.SingleOrDefault(o => o.MAC == n.MAC);

            if (oldNetwork == null)
            {
                return;
            }

            oldNetwork.MAC = n.MAC;
            oldNetwork.status = n.Status;
            oldNetwork.assetID = n.AssetID;
            oldNetwork.latestUpdateDate = n.LatestUpdateDate;
            asset oldAsset = dbContext.asset.SingleOrDefault(o => o.assetID == n.AssetID);
            if (oldAsset == null)
                return;
            oldAsset.belongto = n.Alias;
            dbContext.SaveChanges();
        }
        public void bacthUpdate(string[] MACList, networkInfo n)
        {
            foreach (string MAC in MACList)
            {
                var d = dbContext.network.SingleOrDefault(o => o.MAC == MAC);
                if (d != null)
                {
                    d.latestUpdateDate = n.LatestUpdateDate;
                    d.status = n.Status;
                }
            }
            dbContext.SaveChanges();
        }
        #region Auto mail
        public List<network> getPreOverdueNWInfo_By_Fixeddate(DateTime date1, DateTime date2)
        {
            lock (lock_flag)
            {
                var query = from n in dbContext.network
                            where n.status == 2 || (n.latestUpdateDate > date2 && n.latestUpdateDate < date1)
                            select n;
                return query.ToList();
            }
        }
        public List<network> getOverduedNWInfo_By_Fixeddate(DateTime date)
        {
            lock (lock_flag)
            {
                var query = from n in dbContext.network
                            where n.status == 3 || n.latestUpdateDate < date
                            select n;
                return query.ToList();
            }
        }
        public void update_NetworkStatus_By_MAC(string mac, byte status)
        {
            lock (lock_flag)
            {
                network nw = dbContext.network.SingleOrDefault(o => o.MAC == mac);
                if (nw == null)
                {
                    return;
                }
                if (nw.status != status)
                {
                    nw.status = status;
                    dbContext.SaveChanges();
                }
            }
        }
        public networkInfo_mail getNetworkInfoForMail_By_MAC(string mac)
        {
            lock (lock_flag)
            {
                var query = from u in dbContext.aspnet_Users
                            join m in dbContext.aspnet_Membership on u.UserId equals m.UserId
                            join a in dbContext.asset on u.UserName equals a.belongto
                            join n in dbContext.network on a.assetID equals n.assetID
                            where n.MAC == mac
                            select new networkInfo_mail
                            {
                                MAC = n.MAC,
                                assetID = a.assetID,
                                owner = u.UserName,
                                email = m.Email
                            };
                return query.FirstOrDefault();
            }
        }
        #endregion
    }
}
